<?

function normalize_float($f) {
     return rtrim(trim($f, '0'), '.');
}

function us_state_abbr($input) { 
    //reset found 
    $found = 0; 
    //list states 
    $states = "Alaska,Alabama,Arkansas,Arizona,".
              "California,Colorado,Connecticut,Delaware,".
              "Florida,Georgia,Hawaii,Iowa,Idaho,Illinois,".
              "Indiana,Kansas,Kentucky,Louisiana,Massachusetts,".
              "Maryland,Maine,Michigan,Minnesota,Missouri,".
              "Mississippi,Montana,North Carolina,North Dakota,".
              "Nebraska,New Hampshire,New Jersey,New Mexico,Nevada,".
              "New York,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,".
              "South Carolina,South Dakota,Tennessee,Texas,Utah,".
              "Virginia,Vermont,Washington,Wisconsin,West Virginia,Wyoming"; 

    //list abbreviations 
    $abb = "AK,AL,AR,AZ,CA,CO,CT,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,".
           "MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,N Y,OH,OK,OR,PA,RI,SC,SD,TN,".
           "TX,UT,VA,VT,WA,WI,WV,WY"; 

    //create arrays 
    $states_array = explode(",", $states); 
    $abb_array = explode(",", $abb); 
     
    //run test 
    for ($i = 0; $i < count($states_array); $i++){ 
        if (strtolower($input) == strtolower($states_array[$i])){ 
            $found = 1; 
            $output = $abb_array[$i]; 
            return $output; 
        } 
    } 
    if ($found != 1){ 
        $output = $input; 
        return $output; 
    } 
    return $output; 
}  
 
function IP() { 

    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
 
    return trim($ip);
}


function IP2() { 

    foreach (array('HTTP_CLIENT_IP', 
                   'HTTP_X_FORWARDED_FOR', 
                   'HTTP_X_FORWARDED', 
                   'HTTP_X_CLUSTER_CLIENT_IP', 
                   'HTTP_FORWARDED_FOR', 
                   'HTTP_FORWARDED', 
                   'REMOTE_ADDR') as $key) {

        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return trim(strtolower($ip));
                }
            }
        }
    }
}

 

function animated_gif($filename) {
    if(!($fh = @fopen($filename, 'rb')))
        return false;
    $count = 0;
    //an animated gif contains multiple "frames", with each frame having a
    //header made up of:
    // * a static 4-byte sequence (\x00\x21\xF9\x04)
    // * 4 variable bytes
    // * a static 2-byte sequence (\x00\x2C)

    // We read through the file til we reach the end of the file, or we've found
    // at least 2 frame headers
    while(!feof($fh) && $count < 2) {
        $chunk = fread($fh, 1024 * 100); //read 100kb at a time
        $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
    }

    fclose($fh);
    return $count > 1;
}


function strlcs($string_1, $string_2) {
        $string_1_length = strlen($string_1);
        $string_2_length = strlen($string_2);
        $return          = array();
 
        if ($string_1_length === 0 || $string_2_length === 0)
        {
                // No similarities
                return $return;
        }
 
        $longest_common_subsequence = array();
 
        // Initialize the CSL array to assume there are no similarities
        for ($i = 0; $i < $string_1_length; $i++)
        {
                $longest_common_subsequence[$i] = array();
                for ($j = 0; $j < $string_2_length; $j++)
                {
                        $longest_common_subsequence[$i][$j] = 0;
                }
        }
 
        $largest_size = 0;
 
        for ($i = 0; $i < $string_1_length; $i++)
        {
                for ($j = 0; $j < $string_2_length; $j++)
                {
                        // Check every combination of characters
                        if ($string_1[$i] === $string_2[$j])
                        {
                                // These are the same in both strings
                                if ($i === 0 || $j === 0)
                                {
                                        // It's the first character, so it's clearly only 1 character long
                                        $longest_common_subsequence[$i][$j] = 1;
                                }
                                else
                                {
                                        // It's one character longer than the string from the previous character
                                        $longest_common_subsequence[$i][$j] = $longest_common_subsequence[$i - 1][$j - 1] + 1;
                                }
 
                                if ($longest_common_subsequence[$i][$j] > $largest_size)
                                {
                                        // Remember this as the largest
                                        $largest_size = $longest_common_subsequence[$i][$j];
                                        // Wipe any previous results
                                        $return       = array();
                                        // And then fall through to remember this new value
                                }
 
                                if ($longest_common_subsequence[$i][$j] === $largest_size)
                                {
                                        // Remember the largest string(s)
                                        $return[] = substr($string_1, $i - $largest_size + 1, $largest_size);
                                }
                        }
                        // Else, $CSL should be set to 0, which it was already initialized to
                }
        }
 
        // Return the list of matches
        return $return;
}



function rstring($length) {

    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }

    return $string;
}

function starts_with($haystack, $needle)
{
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
}

function ends_with($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}


function valid_email($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>